package com.jdcjk.project.login.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.jdcjk.Constant;
import com.jdcjk.R;
import com.jdcjk.UrlAddress;
import com.jdcjk.base.presenter.BasePresenter;
import com.jdcjk.base.view.BaseActivity;
import com.jdcjk.project.login.model.entity.LoginResult;
import com.jdcjk.project.login.presenter.LoginPresenter;
import com.jdcjk.project.main.view.MainActivity;
import com.jdcjk.util.PreferencesUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by Administrator on 2016/12/15 0015.
 * 欢迎界面
 */

public class WelcomeActivity extends BaseActivity<LoginPresenter> {
    private final String TAG = WelcomeActivity.class.getSimpleName();
    private LoginPresenter loginPresenter;
    private Handler handler = new Handler();

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public LoginPresenter bindPresenter() {
        loginPresenter = new LoginPresenter(getBaseContext());
        return loginPresenter;
    }

    @Override
    public void initContentView(Bundle savedInstanceState) {

        // 判断当前任务是否为最顶上的Activity
        if (!this.isTaskRoot()) {
            Intent mainIntent = getIntent();
            String action = mainIntent.getAction();
            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }
        //判断是否已经有登录账号，有的话则直接登录
        final String account = PreferencesUtils.getString(this, Constant.UserInfo.account);
        final String passward = PreferencesUtils.getString(this, Constant.UserInfo.passward);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //判断是否有登陆过的账号，没有就等待两秒跳转到登录界面，有则登录
                if (account.trim().equals("") || passward.trim().equals("")) {
                    Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                    return;
                }
                //自动登录
                loginPresenter.login(UrlAddress.LOGIN, account, passward, new BasePresenter.OnUIThreadListener<LoginResult>() {
                    @Override
                    public void onBefore(Request request, int id) {

                    }

                    @Override
                    public void onAfter(int id) {

                    }

                    @Override
                    public void onError(Call call, Exception e, int i) {
                        Toast.makeText(WelcomeActivity.this, R.string.auto_login_error, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onResponse(LoginResult result) {
                        Intent intent;
                        switch (result.getStatus()) {
                            case "OK":
                                Map map = new HashMap();
                                //如果登录成功,将用户信息保存在SharedPreferences
                                map.put(account, account);
                                map.put(Constant.UserInfo.passward, passward);
                                map.put(Constant.UserInfo.username, result.getResult().getUsername());
                                map.put(Constant.UserInfo.email, result.getResult().getEmail());
                                map.put(Constant.UserInfo.address, result.getResult().getAddress());
                                map.put(Constant.UserInfo.id, result.getResult().getId());
                                map.put(Constant.UserInfo.phone, result.getResult().getAccount());
                                PreferencesUtils.saveData(WelcomeActivity.this, map);
                                // 跳转到主界面
                                intent = new Intent(WelcomeActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                                break;
                            default:
                                Toast.makeText(WelcomeActivity.this, R.string.auto_login_fail, Toast.LENGTH_SHORT).show();
                                intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                                break;
                        }
                    }
                });
            }
        }, 2000);

    }
}
